package com.htu.courseService.Talk.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.htu.courseService.Talk.dao.TalkFileMapper;
import com.htu.courseService.Talk.dao.TalkInfoMapper;
import com.htu.courseService.Talk.entity.TalkInfo;
import com.htu.courseService.Talk.service.TalkInfoService;
import com.htu.courseService.Talk.vo.TalkFileDto;
import com.htu.courseService.Talk.vo.TalkInfoDto;
import com.htu.courseService.Talk.vo.TalkListDto;
import com.htu.courseService.Talk.vo.TalkQuery;
import com.htu.courseService.common.enums.RedisCachePrefix;
import com.htu.courseService.common.util.MinioUtil;
import com.htu.courseService.common.util.UserUtil;
import com.htu.courseService.user.vo.UserInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

@Service
@RequiredArgsConstructor
@Slf4j
public class TalkInfoServiceImpl implements RedisCachePrefix, TalkInfoService {
    private final MinioUtil minioUtil;
    private final TalkInfoMapper talkInfoMapper;
    private final TalkFileMapper talkFileMapper;

    @Override
    public TalkFileDto uploadFile(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        String fileUrl = minioUtil.upload(file);
        String originalFilename = file.getOriginalFilename();
        String fileType = "";
        if (originalFilename != null && originalFilename.contains(".")) {
            fileType =  originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        }
        TalkFileDto talkFileDto = new TalkFileDto();
        talkFileDto.setFileUrl(fileUrl);
        talkFileDto.setFileName(fileName);
        talkFileDto.setFileType(fileType);
        return talkFileDto;
    }

    @Override
    public void add(TalkInfoDto talkInfoDto) {
        UserInfo userInfo = UserUtil.getUserInfo();
        TalkInfo talkInfo = new TalkInfo();
        talkInfo.setTitle(talkInfoDto.getTitle());
        talkInfo.setContent(talkInfoDto.getContent());
        talkInfo.setCreatedAt(new Date());
        talkInfo.setCreatedBy(userInfo.getId());
        talkInfoMapper.insert(talkInfo);
    }

    @Override
    public IPage<TalkListDto> list(TalkQuery talkQuery) {
        IPage<TalkListDto> dtoList = talkInfoMapper.getTalkList(new Page<>(talkQuery.getPageNum(),
                talkQuery.getPageSize()),talkQuery.getTitle());
        return dtoList;
    }

    @Override
    public TalkInfoDto getTalkDetail(Integer id) {
        return talkInfoMapper.getTalkDetail(id);
    }

    @Override
    public IPage<TalkListDto> getMyTalkList(TalkQuery talkQuery) {
        UserInfo userInfo = UserUtil.getUserInfo();
        IPage<TalkListDto> dtoList = talkInfoMapper.getMyTalkList(new Page<>(talkQuery.getPageNum(),
                talkQuery.getPageSize()),talkQuery.getTitle(),userInfo.getId());
        return dtoList;
    }

    @Override
    public IPage<TalkListDto> getMyCommentList(TalkQuery talkQuery) {
        UserInfo userInfo = UserUtil.getUserInfo();
        IPage<TalkListDto> dtoList = talkInfoMapper.getMyCommentList(new Page<>(talkQuery.getPageNum(),
                talkQuery.getPageSize()),talkQuery.getTitle(),userInfo.getId());
        return dtoList;
    }
}
